package com.probox.modules.controller.order;

import com.probox.common.core.utils.poi.ExcelUtil;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.entity.service.customer.ICustomerCollectService;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.order.OrderShopCart;
import com.probox.common.entity.service.order.IOrderShopCartService;
import com.probox.customer.api.domain.CustomerCollect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 购物车明细Controller
 *
 * @author hds
 * @date 2021-05-28
 */
@RestController
@RequestMapping("/order/cart")
public class OrderShopCartController extends BaseController {
    @Autowired
    private IOrderShopCartService orderShopCartService;

    @Autowired
    private ICustomerCollectService customerCollectService;

    /**
     * 查询购物车明细列表
     */
    @PreAuthorize(hasPermi = "order:cart:list")
    @GetMapping("/list")
    public TableDataInfo list(CustomerCollect customerCollect) {
        if (customerCollect.getCreateTimeArray() != null && customerCollect.getCreateTimeArray().length>0){
            customerCollect.setStartDate(customerCollect.getCreateTimeArray()[0]);
            customerCollect.setEndDate(customerCollect.getCreateTimeArray()[1]+" 23:59:59");
        }
        startPage();
        List<CustomerCollect> list = customerCollectService.queryList(customerCollect);
        return getDataTable(list);
    }

    /**
     * 导出购物车明细列表
     */
    @PreAuthorize(hasPermi = "order:cart:export")
    @Log(title = "购物车明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderShopCart orderShopCart) throws IOException {
        List<OrderShopCart> list = orderShopCartService.queryList(orderShopCart);
        ExcelUtil<OrderShopCart> util = new ExcelUtil<OrderShopCart>(OrderShopCart.class);
        util.exportExcel(response, list, "购物车明细数据");
    }

    /**
     * 获取购物车明细详细信息
     */
    @PreAuthorize(hasPermi = "order:cart:query")
    @GetMapping(value = "/{cartUid}")
    public AjaxResult getInfo(@PathVariable("cartUid") Long cartUid) {
        return AjaxResult.success(orderShopCartService.selectOrderShopCartById(cartUid));
    }

    /**
     * 新增购物车明细
     */
    @PreAuthorize(hasPermi = "order:cart:add")
    @Log(title = "购物车明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderShopCart orderShopCart) {
        return toAjax(orderShopCartService.insertOrderShopCart(orderShopCart));
    }

    /**
     * 修改购物车明细
     */
    @PreAuthorize(hasPermi = "order:cart:edit")
    @Log(title = "购物车明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderShopCart orderShopCart) {
        return toAjax(orderShopCartService.updateOrderShopCart(orderShopCart));
    }

    /**
     * 删除购物车明细
     */
    @PreAuthorize(hasPermi = "order:cart:remove")
    @Log(title = "购物车明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cartUids}")
    public AjaxResult remove(@PathVariable Long[] cartUids) {
        return toAjax(orderShopCartService.deleteOrderShopCartByIds(cartUids));
    }
}
